package ro.gateway.aida.charts;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.utils.HttpUtils;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 17, 2004
 * Time: 6:53:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChartWizzard$ActionForm extends ActionForm {
	public Object[][] series;
	public String chart_type = "bar";
	public String x_axe_criteria;
	public String x_axe_group_criteria;
	public String y_axe_criteria;
	public String search_query;
	public List search_results;
	public String file_name;
	public String tooltipmap;
	public int chartwidth = 800;
	public int chartheight = 600;

	public int getChartwidth() {
		return chartwidth;
	}

	public void setChartwidth(int chartwidth) {
		this.chartwidth = chartwidth;
	}

	public int getChartheight() {
		return chartheight;
	}

	public void setChartheight(int chartheight) {
		this.chartheight = chartheight;
	}

	public Object[][] getSeries() {
		return series;
	}

	public void setSeries(Object[][] series) {
	}

	public String getChart_type() {
		return chart_type;
	}

	public void setChart_type(String chart_type) {
	}

	public String getX_axe_criteria() {
		return x_axe_criteria;
	}

	public void setX_axe_criteria(String x_axe_criteria) {
	}

	public String getX_axe_group_criteria() {
		return x_axe_group_criteria;
	}

	public void setX_axe_group_criteria(String x_axe_group_criteria) {
	}

	public String getY_axe_criteria() {
		return y_axe_criteria;
	}

	public void setY_axe_criteria(String y_axe_criteria) {
	}

	public String populate(HttpServletRequest request,
					   ServletContext application) {
		PersistenceToken token = PersistenceManager.tokenLookup(application);

		String page = HttpUtils.getValidTrimedString(request, "page", "1");
//		System.out.println("populate page:" + page);
		if ("1".equals(page)) {
			String charttype = HttpUtils.getValidTrimedString(request, "chart_type", null);
			String y_axe = HttpUtils.getValidTrimedString(request, "y_axe_criteria", null);

			if (charttype == null || y_axe == null) {
				return "1";
			}

			this.chart_type = charttype;
			this.y_axe_criteria = y_axe;

			return "2";
		} else if ("2".equals(page)) {
			String x_axe = HttpUtils.getValidTrimedString(request, "x_axe_criteria", null);
			if (x_axe == null) {
				return "2";
			}
			this.x_axe_criteria = x_axe;

			if (this.chart_type.startsWith("bar")) {
				return "3";
			} else {
				try {
					ChartRenderer.make_chart(request, application, this);
				} catch (IOException ioex) {
					ioex.printStackTrace();
					return null;
				} catch (ChartException chart_exception) {
					chart_exception.printStackTrace();
					return "1";
				}
				return "chart";
			}
		} else if ("3".equals(page)) {
			this.x_axe_group_criteria = HttpUtils.getValidTrimedString(request, "x_axe_group_criteria", null);

			try {
				ChartRenderer.make_chart(request, application, this);
			} catch (IOException ioex) {
				ioex.printStackTrace();
				return null;
			} catch (ChartException chartException) {
				chartException.printStackTrace();
				return "1";
			}
			return "chart";
		}
//		System.out.println("page null");
		return null;
	}


	public static String[][] getCategoryKeys() {
		return categories_keys;
	}


	private static final String[][] categories_keys = new String[][]{
		{"countries", "string"},
		{"counties", "string"},
		{"regions", "string"},
		{"towns", "string"},
		{"sectors", "string"},
		{"funding_orgs", "string"},
		{"implementing_orgs", "string"},
		{"executing_orgs", "string"},
		{"funding_status", "string"},
		{"types_assistance", "string"},
		{"terms_assistance", "string"},
		{"proposed_approval", "date"},
		{"actual_approval", "date"},
		{"proposed_starting", "date"},
		{"actual_starting", "date"},
		{"status", "string"}
	};
}
